package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MyCalendar extends JFrame {

	private JPanel firstpage;
	private JPanel panel;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTextArea result;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MyCalendar frame = new MyCalendar();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MyCalendar() {
		setTitle("MyCalendar");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 500, 500);
		firstpage = new JPanel();
		firstpage.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(firstpage);
		firstpage.setLayout(new BorderLayout(0, 0));
		firstpage.add(getPanel(), BorderLayout.NORTH);
		firstpage.add(getScrollPane(), BorderLayout.CENTER);
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.add(getBtnNewButton());
		}
		return panel;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("Today");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Calendar c1 = Calendar.getInstance();
					String str = null;
					// 년월일
					int y = c1.get(Calendar.YEAR);
					int m = c1.get(Calendar.MONTH)+1;
					int d = c1.get(Calendar.DATE);
					String temp = String.format("년도 : %10d", y);	
					
					result.append(y+" 년 ");
					result.append(m+" 월 ");
					result.append(d+" 일 \n");
					//result.append(temp+"\n");
					
					// 시분초
					int h = c1.get(Calendar.HOUR_OF_DAY);
					int mm = c1.get(Calendar.MINUTE);
					int s = c1.get(Calendar.SECOND);
					result.append(h+" 시 ");
					result.append(mm+" 분 ");
					result.append(s+" 초 \n");
					
					// SimpleDateFormat
					SimpleDateFormat sdf = new SimpleDateFormat("yy 년 MM 월 dd 일 (E요일)");
					String sdfdate = sdf.format(new Date());
					result.append(sdfdate+"\n");
					
					
					
				}
			});
		}
		return btnNewButton;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}
}
